<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\PaperModel;
use cmf\controller\AdminBaseController;
use think\Db;

class PaperController extends AdminBaseController
{

	private function getstatus(){
		return ['0'=>'未开始','1'=>'已开始','2'=>'已结束'];
	}

	
    public function index()
    {
        $map    = [];
		$data   = $this->request->param();
        $listid = isset($data['status']) && $data['status'] ? $data['status'] : '';
        if ($listid != '') {
            $map[] = ['status', '=', $status];
        }
		$list = DB::name('ask_list')->where($map)->order("list_order asc")->paginate(10, false, ['query' => input()]);
        $list->each(function ($v, $k) {
			$v['status'] = $v['endtime']>time() ? $v['status'] : '2';
            return $v;
        });		
		$page = $list->render();
		$this->assign("page", $page);
		$this->assign("status", $this->getstatus());
        $this->assign('list', $list);

        return $this->fetch();
    }


    public function add()
    {

        return $this->fetch();
    }


    public function addPost()
    {
        if ($this->request->isPost()) {
            $data           = $this->request->param();
            $validate         = $this->validate($data, 'Paper');
            if ($validate !== true) {
                $this->error($validate);
            }				
			$data['addtime']=time();
			$data['endtime']=strtotime($data['endtime']);
            $result         = DB::name('ask_list')->insert($data);
            if (!$result) {
                $this->error($result);
            }
            $this->success("添加成功！", url("paper/index"));
        }
    }

    public function edit()
    {
        $id             = $this->request->param('id');
        $result          = DB::name('ask_list')->where('id',$id)->find();
        $this->assign('result', $result);

        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
			
            $data   = $this->request->param();
            $validate         = $this->validate($data, 'Paper');
            if ($validate !== true) {
                $this->error($validate);
            }				
			$data['addtime']=time();
			$data['endtime']=strtotime($data['endtime']);
			
			$check = DB::name('ask_list')->where('id',$data['id'])->find();
			if($check['status']==1){
				$this->error('该问卷已启用，不能被修改');
			}

            $slidePostModel = DB::name('ask_list')->where('id',$data['id'])->update($data);
			
			if($slidePostModel){
				$this->success("保存成功！", url("paper/index"));
			}
             $this->error('信息错误');
        }
    }

    public function setstatus()
    {
        $id             = $this->request->param('id');
        $result          = DB::name('ask_list')->where('id',$id)->update(['status'=>1]);
		if($result){
			$this->success("保存成功！", url("paper/index"));
		}
		 $this->error('信息错误');
    }

    public function cancelstatus()
    {
        $id             = $this->request->param('id');
        $result          = DB::name('ask_list')->where('id',$id)->update(['status'=>0]);
		if($result){
            // DB::name('user_ask')->where('listid',$id)->delete();
            // $list = DB::name('ask')->where('listid',$id)->select()->toArray();
            // foreach($list as $k=>$v){
            //     DB::name('user_ask_list')->where('askid',$v['id'])->delete();
            // }
			$this->success("保存成功！", url("paper/index"));
		}
		 $this->error('信息错误');
    }

    public function delete()
    {
        if ($this->request->isPost()) {
            $id             = $this->request->param('id', 0, 'intval');
			if(DB::name('ask_list')->where('id',$id)->delete()){
				//清除下属问题
				DB::name('ask')->where('listid',$id)->delete();
				//清除下属问卷
				DB::name('user_ask')->where('listid',$id)->delete();
				
				$this->success("删除成功！", url("paper/index"));
			}
            $this->error('信息错误');
        }
    }

    public function listOrder()
    {
        $noticepageModel = new  PaperModel();
        parent::listOrders($noticepageModel);
        $this->success("排序更新成功！");
    }	
}
